﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using CommonClass;
using CommonBase;


namespace SurveillanceLib
{
    /// <summary>
    /// 用于视频界面的分屏，具备关闭与打开视频功能
    /// </summary>
    public class PanelOperation
    {
        #region 属性

        /// <summary>
        /// 记录所选 Panel，只能选择一个 Panel【Static】
        /// </summary>
        public static Panel SelectedPanel { get; set; }

        #endregion

        #region 方法

        /// <summary>
        /// 将 SelectedPanel 设为 null
        /// </summary>
        public void SetNull2SelectedPanel()
        {
            SelectedPanel = null;
        }

        /// <summary>
        /// 分屏
        /// </summary>
        /// <param name="screenCount">分屏个数</param>
        /// <param name="splitScreen">基窗口</param>
        /// <param name="maxScreen">最大化窗口</param>
        /// <param name="currentList">记录大屏已经创建好的 Panel 数组 | 相关列表，主要用于修改新创建的小分屏</param>
        /// <param name="doubleAction">共同的双击事件</param>
        /// <param name="isShowLabelAndColor">是否在 Panel 上显示标签与背景色</param>
        /// <param name="toolTip">ToolTip 实例</param>
        /// <returns>返回最新分屏列表</returns>
        public List<Panel> SplitScreens(int screenCount, Panel splitScreen, Panel maxScreen,
            List<Panel> currentList, Action<bool> doubleAction = null,
            bool isShowLabelAndColor = false, ToolTip toolTip = null)
        {
            try
            {
                int outChannel;
                SplitScreen splitscreen = new SplitScreen(); // 画面分割
                List<Panel> panelCurrentList = new List<Panel>(); // 记录大屏新创建的 Panel 数组
                List<Panel> panelHistoryList = new List<Panel>(); // 记录上一次系统创建的 Panel 数组
                IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(screenCount, splitScreen.Size);

                splitScreen.Controls.Clear(); // 先清除 Panel 已存在的内容
                // 1. 将当前正在播放的视频停止
                if (currentList != null)
                {
                    foreach (Panel currPanel in currentList)
                    {
                        if (currPanel.Tag != null &&
                            !string.IsNullOrWhiteSpace((currPanel.Tag as TagModel).Panel_RealPlayHandle))
                        {
                            TagModel currPanelTag = currPanel.Tag as TagModel;
                            if (currPanelTag.Driver_Instance.
                                StopRealPlay(currPanelTag.Panel_RealPlayHandle)) // 停止实时监控，需要传递实时监控句柄
                                currPanelTag.Panel_RealPlayHandle = string.Empty;
                            else
                            {
                                MessageBox.Show(SurveillanceUtilities.STOP_REALPLAY_FAILURE, ConstantString.MSG_COMPANY);
                                return null;
                            }
                        }

                        panelHistoryList.Add(currPanel); // 2. 将已停止的视频数组放入历史数组中
                    }
                }

                for (int i = 0; i < rectList.Count; i++)
                {
                    Panel panel = new Panel()
                    {
                        BackColor = Color.Black,
                        Width = rectList[i].Width,
                        Height = rectList[i].Height,
                        Left = rectList[i].X,
                        Top = rectList[i].Y,
                        Padding = new Padding(0),
                        Margin = new Padding(0),
                        Tag = new TagModel()
                        {
                            Panel_Index = i,
                        },
                    };

                    // 3. 处理 panel 的相关信息
                    if (currentList != null && i < currentList.Count && currentList[i] != null &&
                        currentList[i].Tag != null && currentList[i].Tag is TagModel &&
                        !string.IsNullOrWhiteSpace((currentList[i].Tag as TagModel).Panel_SelectedChannel)) // 所选通道不为空时
                    {
                        if ((currentList[i].Tag as TagModel).Driver_IsLogin) // 设备已登录
                            SurveillanceUtilities.SetPanelTooltip(panel, currentList[i].Tag as TagModel, toolTip, isShowLabelAndColor, Color.LimeGreen); // 设置 Tooltip
                        else
                            SurveillanceUtilities.SetPanelTooltip(panel, currentList[i].Tag as TagModel, toolTip, isShowLabelAndColor, Color.Red, "设备登录失败"); // 设置 Tooltip
                    }

                    if (!isShowLabelAndColor)
                    {
                        // 4. 在新分屏数组中播放已保存的历史视频
                        if (panelHistoryList != null && panelHistoryList.Count > 0 &&
                            i < panelHistoryList.Count && panelHistoryList[i] != null &&
                            panelHistoryList[i].Tag != null &&
                            panelHistoryList[i].Tag is TagModel)
                        {
                            TagModel tag = panelHistoryList[i].Tag as TagModel;
                            if ((panelHistoryList[i].Tag as TagModel).Driver_Instance != null &&
                                !string.IsNullOrWhiteSpace((panelHistoryList[i].Tag as TagModel).Driver_LoginID) &&
                                !string.IsNullOrWhiteSpace((panelHistoryList[i].Tag as TagModel).Panel_SelectedChannel) &&
                                int.TryParse((panelHistoryList[i].Tag as TagModel).Panel_SelectedChannel, out outChannel) &&
                                string.IsNullOrWhiteSpace((panelHistoryList[i].Tag as TagModel).Panel_RealPlayHandle)) // 未播放时
                            {
                                string pRealPlayHandle = tag.Driver_Instance.RealPlay(tag.Driver_LoginID,
                                    outChannel, panel.Handle); // 只取数据不播放
                                tag.Panel_RealPlayHandle = pRealPlayHandle; // 得到最新的 RealPlayHandle
                            }

                            Utilities.CopyObject(tag, panel.Tag);
                        }


                        // 单击时
                        panel.Click += (s, e) =>
                        {
                            SelectedPanel = s as Panel;
                            splitScreen.Refresh(); // 重刷界面：将边框颜色重置
                            Utilities.PaintPanelBorder(s as Panel, Color.LightGreen, 2, ButtonBorderStyle.Solid); // 绘制边框
                        };

                        // 双击时最大化
                        panel.DoubleClick += (s, e) =>
                        {
                            SelectedPanel = s as Panel;
                            if ((s as Panel).Tag != null &&
                                (s as Panel).Tag is TagModel &&
                                string.IsNullOrWhiteSpace(((s as Panel).Tag as TagModel).Panel_RealPlayHandle))
                            {
                                return; // 必须在播放情况下才能最大化
                            }

                            splitScreen.Visible = false;
                            maxScreen.Visible = !splitScreen.Visible;

                            if (maxScreen.Tag == null)
                                maxScreen.Tag = new TagModel();

                            SurveillanceUtilities.StopAndPlay(s as Panel, maxScreen);
                            SurveillanceUtilities.SetPanelTooltip(maxScreen, SelectedPanel.Tag as TagModel, toolTip, isShowLabelAndColor, Color.LimeGreen); // 设置 Tooltip

                            // 执行双击后的相关操作
                            if (doubleAction != null)
                                doubleAction(true);
                        };
                    }

                    splitScreen.Controls.Add(panel);
                    panelCurrentList.Add(panel); // 5. 加入到当前 Panel 列表
                }

                return panelCurrentList;
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return null;
            }
        }

        #endregion
    }
}
